package com.example.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.demo.service.LoginService;
import com.example.demo.untils.XString;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONObject;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LoginActivity extends AppCompatActivity {


    private EditText mUsernameEditText;
    private EditText mPassEditText;
    private Button mLoginButton;
    private Button mRegistryButton;


    private String mUsername;
    private String mPassword;

    private LoginService mLoginService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化登录的服务类，用该服务类发起请求
        mLoginService = new LoginService();

        setContentView(R.layout.activity_login);
        ActionBar action = getSupportActionBar();
        if(action != null){
            action.hide();
        }
        mUsernameEditText = findViewById(R.id.username);
        mPassEditText = findViewById(R.id.password);
        mLoginButton = findViewById(R.id.loginButton);
        mRegistryButton = findViewById(R.id.registryButton);

        loadUsername();
        loadPassword();
        loadLoginButton();
        loadRegistryButton();
    }

    public void loadUsername(){
        mUsernameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            //得到输入的user
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mUsername = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    public void loadPassword(){
        mPassEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            //得到输入的密码
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPassword = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void loadLoginButton(){
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (XString.isEmpty(mUsername)) {
                    Toast.makeText(LoginActivity.this,"请输入账号",Toast.LENGTH_LONG).show();
                    return;
                }
                if (XString.isEmpty(mPassword)) {
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_LONG).show();
                    return;
                }
                //点击登录按钮 将用户和密码 还有 实现登录逻辑
                mLoginService.login(mUsername,mPassword,new LoginHandler());
            }
        });
    }

    public void loadRegistryButton(){
        mRegistryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegistryActivity.class);
                startActivity(intent);
            }
        });
    }

    private class LoginHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            String result = msg.getData().getString("result");

            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            JsonArray map = json.get("data").getAsJsonArray();
            
            String resultMsg = json.get("msg").getAsString();

            JsonObject jsonObject = map.get(0).getAsJsonObject();


//            //得到用户昵称
            String nickname = jsonObject.get("nickname").getAsString();
//            //得到用个性签名
           String talk = jsonObject.get("talk").isJsonNull()?"":jsonObject.get("talk").getAsString();
//            //得到用户输入的地址
            String address = jsonObject.get("address").isJsonNull()?"":jsonObject.get("address").getAsString();
            int code = json.get("code").getAsInt();
            //打印提示信息

            Toast.makeText(LoginActivity.this,resultMsg,Toast.LENGTH_LONG).show();

            //跳转到首页
            if(code==200){//通过code后端传过来的状态码，决定安卓的展示状态。
                Intent intent = new Intent(LoginActivity.this, MineActivity.class);
//
               intent.putExtra("username", nickname);
               intent.putExtra("talk", talk);
               intent.putExtra("address", address);

                startActivity(intent);
            }
        }
    }

}